package es.udc.emarketapp.http.controller.actions.clients;

import es.udc.emarketapp.emarket.model.client.vo.ClientVO;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import es.udc.emarketapp.emarket.model.usersFacade.UsersFacade;
import es.udc.emarketapp.emarket.model.usersFacade.util.PasswordEncrypter;
import es.udc.emarketapp.emarket.util.exceptions.model.NifCifAlreadyExistsException;
import es.udc.emarketapp.emarket.util.exceptions.model.UserNameAlreadyExistsException;
import es.udc.emarketapp.http.view.actionForms.ClientForm;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.exceptions.ModelException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;
import java.util.Calendar;
import org.apache.struts.action.ActionRedirect;
import org.apache.struts.config.ForwardConfig;

public class UpdateClientDetailsAction extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        /* Get data. */
        ClientForm clientForm = (ClientForm) form;

        ActionMessages errors = new ActionMessages();

        UsersFacade usersFacade = new UsersFacade();

        ClientVO clientVO = new ClientVO(clientForm.getIdClient(), null,
                clientForm.getName(), clientForm.getSurname1(),
                clientForm.getSurname2(), clientForm.getNifCif(),
                clientForm.getAddress(), clientForm.getCity(),
                clientForm.getCp(), clientForm.getProvince(),
                clientForm.getCountry(), clientForm.getEmail(),
                clientForm.getTlf1(), clientForm.getTlf2(),
                clientForm.getBirthDate(), clientForm.getAddressSend(), clientForm.getCitySend(),
                clientForm.getCpSend(), clientForm.getProvinceSend(),
                clientForm.getCountrySend());


        try {

            usersFacade.modifyClient(clientVO);

        } catch (InstanceNotFoundException e) {

            errors.add("idClient",
                    new ActionMessage("ErrorMessages.client.notFound"));

        }
        /* Return ActionForward. */

        if (errors.isEmpty()) {

            //return mapping.findForward("Success");
            String idClient = clientVO.getIdUser();
            String type = "CLIENT_ID";
            return new ActionForward(mapping.findForward("clientDetails").getPath()+"?identifierType="+type+"&identifier="+idClient, false);

        } else {

            saveErrors(request, errors);
            return new ActionForward(mapping.getInput());

        }

    }
}
